<?php	
	//echo $acStudent;
?>
<script type="text/javascript">
var autoId=<?php echo $autoId; ?>;

function addDetail(){
	var url="index.php?r=keuangan/tu/studentInvoice/invoiceDetailForm";
	var winName = "Form Detail Invoice";
	var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
	var width=400,height=300;
	var replace = false,onCenter = true;
	var w = openWindow(winName,url,width,height,onCenter,specs,replace);
	w.focus();
	var timer = setInterval(function() {   
		if(w.closed) {  
			clearInterval(timer); 
			//requeryOutstanding();
		}  
	}, 1000);
}
function doAddDetail(itemId,itemDesc,notes,amount,qty,disc,total){
	autoId+=1;
	data = {id:autoId,student_item_id:itemId,student_item_desc:itemDesc,notes:notes,total:amount,qty:qty,disc:disc,total_after_disc:total,status:'new'};	
	$("#tblInvDetail").jqGrid("addRowData",autoId,data,"first",0);
	recalculateTotal();
}
function deleteRow(id){	
	$("#tblInvDetail").jqGrid("delRowData",id);
	/*
	var data = $("#tblInvDetail").jqGrid("getRowData",id);
	if (data.status =="new"){
		//$("#tblInvDetail").jqGrid("setCell",id,"status","delete");
		$("#tblInvDetail").jqGrid("delRowData",id);
	}else{
		$("#tblInvDetail").jqGrid("setCell",id,"status","deleted");
		// Hide row
		$("#tblInvDetail #" + id).css("display","none");
	}
	*/
	recalculateTotal();
}
function save(){
	var data= $("#tblInvDetail").jqGrid("getGridParam","data");	
	$("#invDetail").val(JSON.stringify(data));	
	$("#frmData").submit();
}
function recalculateTotal(){
	var ids = jQuery("#tblInvDetail").getDataIDs();
	total = 0;
	disc =0;
	ttlAfterDisc = 0;
	 
	for(var i=0;i<ids.length;i++){
		var data = $("#tblInvDetail").jqGrid("getRowData",ids[i]);
		if (data.status != "deleted"){			
			total += data.total * data.qty;
			disc += data.disc * 1;
		}
	}
	ttlAfterDisc = total - disc;
	$("#total").html(accounting.formatMoney(total));
	$("#disc").html(accounting.formatMoney(disc));
	$("#totalAfterDisc").html(accounting.formatMoney(ttlAfterDisc));	
}
function cancel(){
	window.opener.cancelRefresh = true;
	window.close();
}

$(function(){
	
	$("#student").autocomplete({
		source:<?php echo $acStudent;?>,
		change:function(event,ui){
			if(ui.item == null){
				alert("Invalid Student!");
				$("#student").val("");
				$("#student").focus();
			}else{
				$("#student_id").val(ui.item.id);
				$("#department_id").html(ui.item.department_id);
			}
		}
	});

	$("#total").html(accounting.formatMoney(<?php echo $ttl_invoice; ?>));
	$("#disc").html(accounting.formatMoney(<?php echo $ttl_disc; ?>));
	$("#totalAfterDisc").html(accounting.formatMoney(<?php echo $ttl_invoice - $ttl_disc; ?>));

	$("#tblInvDetail").jqGrid({
		//url:'index.php?r=keuangan/tu/studentInvoice/invoiceDetail',
		//editUrl:'index.php?r=akademik/pendaftaran_kelas/kelas_tahun_ajaran/edit',
		postData:{"id":$("#id").val()},
		data:<?php echo $dInv;?>,
		datatype: 'local',
		loadonce:true,
		mtype:"GET",
		autowidth:true,
		height:"100%",
		altRows:true,
		ignoreCase:true,
		colNames: ['id','Item ID','Item Name','Notes','Amount','Qty','Disc','Total','Status','Action'],
		colModel: [
			{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
			{name: 'student_item_id', index: 'student_item_id', width: 40,search:true},
			{name: 'student_item_desc', index: 'student_item_desc',search:true},
			{name: 'notes', index: 'notes', formatter: 'textarea',width:100},
			{name: 'total', index: 'total', width: 40,template:moneyTemplate},
			{name: 'qty', index: 'qty', width: 40,template:moneyTemplate},
			{name: 'disc', index: 'disc', width: 40,template:moneyTemplate},
			{name: 'total_after_disc', index: 'total_after_disc', width: 40,template:moneyTemplate},
			{name: 'status', index: 'status', width: 40},
			{name: 'act', index: 'act', width: 40, align: 'center'},				
		],
		gridview: true,
		rownumbers: false,
		rowNum: 10,
		rowList: [5, 10, 15],
		pager: '#navTblOut',
		viewrecords: true,
		caption: 'Department',
		jsonReader : { 
			root:"model",
			repeatitems: false			
		},
		loadComplete:function(data){
			console.log(data);
		},
		gridComplete: function(){ 
			var ids = jQuery("#tblInvDetail").getDataIDs(); 
			console.log(ids);
			
			for(var i=0;i<ids.length;i++){ 
				var cl = ids[i]; 
				//btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
				btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
				jQuery("#tblInvDetail").setRowData(ids[i],{act:btnDelete}) 
			} 
		}
	});	
});	
</script>
<div style="width:800px">
	<div id="tabs">
		<div id="single">
			<div style="border-bottom:2px solid #000;padding-bottom:5px;margin-bottom:20px">
			<?php 
				$hideSave = false;
				if ($invoice_type == "Auto"){
					$hideSave = true;
				}
				$this->widget('application.components.widgets.ActionBar', 
					array("menuAlign" => "right", 
						'items' => array(
							array(
								'id' => "btnRefresh",
								'title' => "Simpan",
								'icon' => "images/action/Actions-document-save-all-icon.png",
								'callback'=>"save",
								'hidden'=>$hideSave,
							),					
							array(
								'id' => "btnCancel",
								'title' => "Batal",
								'icon' => "images/action/Actions-dialog-cancel-icon.png",
								'callback'=>"cancel"
							),
						)
					)
				);
			?>
			</div>
			<form id="frmData" action="index.php?r=keuangan/tu/studentInvoice/edit" method="POST">
				<?php
					$labelWidth = "100px";
				?>
				
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Invoice No :</label>
					<input type="text" id="invoice_no" name="invoice_no" value="<?php echo $invoice_no; ?>" />
					<input type="hidden" id="id" name="id" value="<?php echo $id; ?>" />
					<input type="hidden" id="op" name="op" value="<?php echo $op;?>" />
				</div>
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Date :</label>
					<?php
						$this->widget('application.components.widgets.DateTextField', array('name'=>'invoice_date','value'=>$invoice_date));
					?>
				</div>
				
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Transaction type:</label>
					<select id="student_transaction_type_id" name="student_transaction_type_id" style="min-width:300px">
						<?php
							foreach ($arrStudentTransactionType as $studentTransactionType){
								$selected = $student_transaction_type_id == $studentTransactionType->id ? "selected": "";
								echo "<option value ='".$studentTransactionType->id."' $selected>".$studentTransactionType->name."</option>";
							} 
						?>
					</select>
					
				</div>
				
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Student :</label>
					<input type="text" id="student" name="student" style="width:300px" value="<?php echo $student_name;?>"/>
					<input type="hidden" id="student_id" name="student_id" style="width:30px" value="<?php echo $student_id;?>" />
				</div>
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Departemen :</label>
					<label id="department_id" class="form" style="min-height:15px"> <?php echo $department_id;?></label>
				</div>
				<div>
					<label class="form" style="vertical-align:top;text-align:right;width:<?php echo $labelWidth;?>"> Notes :</label>
					<textarea id="notes" name="notes" style="width:650px;height:50px"><?php echo $notes;?></textarea>
				</div>
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Invoice Type :</label>
					<label class="form" style="text-align:right;"> <?php echo $invoice_type; ?></label>
				</div>
				
				<div>
					<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Payment Type :</label>
					<select id="payment_type" name="payment_type">
						<option value="CASH" <?php echo $payment_type =="CASH"?"selected":""; ?>>Cash</option>
						<option value="BCA VA" <?php echo $payment_type =="BCA Virtual Account"?"selected":""; ?>>BCA Virtual Account</option>
					</select>
					
				</div>
				
				<div style="float:right;padding-bottom:0px;margin-bottom:0px">
					<div class="actionBarItem imgBtn" style="float:left;margin-right:15px;" onclick="addDetail()"> 
					<img src="images/action/Actions-list-add-icon.png" height="25" width="25"> 
					<span>Tambah</span>
					</div>
				</div>
				
				<div style="clear:both"></div>
						
				<table id="tblInvDetail"></table>
				<div id="navTblInvDetail"></div>
				<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Total : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="total">0</label>
				</div>
				<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Disc : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="disc">0</label>
				</div>
				<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Total after Disc : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="totalAfterDisc">0</label>
				</div>
				
				</br>
				<textarea id="invDetail" name="invDetail" style="display:none"></textarea>
			</form>		
		</div>
		<div id="bulk">
		</div>
	</div>
	<div>
		
	</div>
</div>